<?php

/*
 * 
 * Catalogue Module: appel au serveur Complus pour la page "Catalogue"
 * 
 * @author	Thierry Brodard
 */

/**********************************************************
 ***** Libs
 **********************************************************/

require_once ('lib/class.http.php');
require_once ('lib/dump.php');
require_once ('lib/class.urls.php');
require_once ('lib/parvis.constants.php');


// Retrouver la langue de l'URL
$langage = urls::getLangNum();

$api = new Http();

$url = 'http://www.parvis.ch/cgi-bin/cgiip.exe/WService=wsbroker1/tbr_article.w';

$api->useCurl(false);


if (empty($_POST['navigue'])) {

	// 1er appel: GET
    $api->setMethod('GET');
    $api->addParam('lang', $langage);
	$api->addParam('langue', $langage);
	
	$search = $_GET['search'];
	if (empty($search)) {
    	$api->addParam('lib', '*');
	} else {
		$api->addParam('lib', utf8_decode($search));
	}

    $mode= $_GET['mode'];
    if (!empty($mode)) {
		if ($mode === "N"){//nouveauté
			$api->addParam('stat', 'N');
		}else if ($mode === "P"){//promotion
			$api->addParam('stat', 'P');
		}else if ($mode === "L"){//en solde
			$api->addParam('stat', 'L');
		}
    }

} else {
	// Appels suivants (via un des boutons de l'interface)
    $api->setMethod('POST');

    // Les paramètres de POST sont redécodés en ISO
    foreach ($_POST as $key => $post) {
	    $param = utf8_decode($_POST[$key]);

	    $api->addParam($key, $param);
	}
    $api->addParam('lang', $langage);
	
}

$api->useCookie(true);
$api->saveCookie(true);
$api->addCookie('SessionId', $_COOKIE['SessionId']);
$api->addCookie('InterNou', $_COOKIE['InterNou']);



$api->execute($url);

$cookies = $api->_cookies;

if ($debug) {
	dump($cookies, 'Cookies retourné par le serveur');

	dump($cookies[0]['value'], 'value');
}

if (!empty($cookies[0])) {
	// Stockage "local" du cookie retourné par le serveur
	if ($cookies[0]['name'] == 'InterNou') {
		setcookie('InterNou', $cookies[0]['value'], COOKIES_TIME, COOKIES_PATH);
	}
	if ($cookies[0]['name'] == 'SessionId') {

		setcookie('SessionId', $cookies[0]['value'], COOKIES_TIME, COOKIES_PATH);
	}
}

echo ($api->error) ? $api->error : utf8_encode($api->result);

?>
